package com.qiyou.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.qiyou.dao.factory.DaoFactory;
import com.qiyou.util.DBUtil;
import com.qiyou.util.LogUtil;

/**
 * 客户端景点列表信息
 * 
 * @author 杨强
 */
@WebServlet("/ScenicSpotsService")
public class ScenicSpotsService extends HttpServlet
{
    private static final long serialVersionUID = 1L;
    private static final Logger log = LogUtil
            .getLogger(ScenicSpotsService.class);

    public ScenicSpotsService()
    {
        super();
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");

        String userID = request.getParameter("userID");
        String sql = "select `tb_scenic_spots`.scsID, `tb_scenic_spots`.scsName, `tb_scenic_spots`.scsType, `tb_scenic_spots`.scsCountry, `tb_scenic_spots`.scsProvince, `tb_scenic_spots`.scsCity, `tb_scenic_spots`.scsNums, `tb_scenic_spots`.scsPath, `tb_scenic_spots`.location, `tb_scenic_spots`.longitude, `tb_scenic_spots`.atitude, if((select count(`tb_place_attestation`.userID) from `tb_place_attestation` where `tb_place_attestation`.sceID = `tb_scenic_spots`.scsID and `tb_place_attestation`.userID = ?) > 0, 1, 0) `认证情况` from `tb_scenic_spots`";

        Connection conn = DaoFactory.getDatabase().getConnection();
        List<Map<String, Object>> result = null;
        try
        {
            result = DBUtil.queryForMap(conn, sql, userID);
        }
        catch (SQLException e)
        {
            log.error(e);
        }
        finally
        {
            DBUtil.closeConnection(conn);
        }

        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        String msg = gson.toJson(result);
        PrintWriter out = response.getWriter();
        out.print(msg);
        out.flush();
        out.close();

        log.info("用户:" + userID + ",访问了景点列表信息:" + msg);
    }
}
